<template>
	<div>
		<el-popconfirm :title="`确定全部推送给吗？`" @confirm="onPushWxEmpBtn">
			<div slot="reference" class="item-gap"><el-button :disabled="disabled" size="mini" icon="el-icon-position">批量推送</el-button></div>
		</el-popconfirm>
	</div>
</template>

<script>
    import {
        msgTemplateSendBatchMsgUser
    } from '../../../../api/production/xsDailyGroupYieldNew';
    import { tipsBar } from '../../../../utils/jk/jk-tool';

    export default {
        props: {
            dataItem: {
                type: Object,
                default() {
                    return {
                        empList: [],
                        scheduleShiftName: '',
                        scheduleGroupName: '',
                        belongDate: ''
                    };
                }
            },
            disabled: {
                type: Boolean,
                default: false
            }
        },
        methods: {
            onPushWxEmpBtn() {
                let hasOpenIdArr = [];
                const unHasOpenIdArr = this.dataItem.empList.filter(x => !x.empOpenId);
                hasOpenIdArr = this.dataItem.empList.filter(x => x.empOpenId);
                hasOpenIdArr = hasOpenIdArr.map(x => {
                    return {
                        ...this.dataItem,
                        'openId': x.empOpenId,
                        'empName': x.empName,
                        'time': `${this.dataItem.belongDate} ${this.dataItem.scheduleShiftName}/${this.dataItem.scheduleGroupName}`,
                        'salary': x.totalSalary,
                        'qty': x.empOutputKg,
                        'machineList': x.machineList
                    };
                });
                if (hasOpenIdArr.length) {
                    if (unHasOpenIdArr.length) {
                        this.$confirm(`${unHasOpenIdArr.map(x => x.empName).join()}, 未绑定微信，确定推送吗?`, { type: 'warning', confirmButtonText: '只推送已绑定的人员' }).then(() => {
                            msgTemplateSendBatchMsgUser(hasOpenIdArr).then(res => {
                                if (res.data.status === 200) {
                                    tipsBar(this, 'custom', 'success', '推送成功!');
                                }
                            });
                        });
                    } else {
                        msgTemplateSendBatchMsgUser(hasOpenIdArr).then(res => {
                            if (res.data.status === 200) {
                                tipsBar(this, 'custom', 'success', '推送成功!');
                            }
                        });
                    }
                } else {
                    tipsBar(this, 'custom', 'warning', '没有绑定微信的人员，无法推送!');
                }
            }
        }
    };
</script>
